package com.jiukuaidao.zhulin.module.user;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jiukuaidao.library.base.BaseActivity;
import com.jiukuaidao.library.util.LogUtil;
import com.jiukuaidao.library.util.ToastUtil;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.data.TaskProvider;
import com.jiukuaidao.zhulin.module.CommWatcher;
import com.jiukuaidao.zhulin.module.user.contract.LoginContract;
import com.jiukuaidao.zhulin.module.user.presenter.LoginPresenter;

import butterknife.BindView;

/**
 * Author : PaiTaSuo
 * Date : 2016/5/25
 * Description :用户登录
 */
public class LoginActivity extends BaseActivity<LoginContract.Presenter> implements View.OnClickListener, LoginContract.View {


    @BindView(R.id.titile_left_imageview)
    ImageView mLeftImg;
    @BindView(R.id.titile_text)
    TextView mTitileText;
    @BindView(R.id.titile_right_text)
    TextView mTitileRightText;
    @BindView(R.id.et_login_username)
    EditText mEtUName;
    @BindView(R.id.et_login_password)
    EditText mEtPassword;
    @BindView(R.id.bt_login)
    Button mBtnLogin;
    @BindView(R.id.tv_forget_password)
    TextView mForgetPassword;
    @BindView(R.id.tv_phone_login)
    TextView mPhoneLogin;

    private Bundle mBundle;
    public static LoginActivity mInstance;


    private String mFromActivity; // 用于判断登录成功返回哪个页面

    @Override
    protected View getTargetView() {
        return null;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void handleBundle(Bundle bundle) {
        mBundle = bundle;
    }

    @Override
    protected void init() {
        mInstance = this;
        mTitileText.setText(R.string.user_login_title);
        mTitileRightText.setText(R.string.user_register_title);
        new LoginPresenter(TaskProvider.provideTasksRepository(this), this);
        setOnClickListener();
        // setSystemBarColor();
        mEtPassword.addTextChangedListener(new CommWatcher(mBtnLogin, CommWatcher.PWDLOGINBTN));
    }

    public void setOnClickListener() {
        mBtnLogin.setOnClickListener(this);
        mPhoneLogin.setOnClickListener(this);
        mForgetPassword.setOnClickListener(this);
        mTitileRightText.setOnClickListener(this);
        mLeftImg.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_login:
                mPresenter.start();
                break;
            case R.id.tv_phone_login:
                intentJumpForResult(LoginQuickActivity.class, 0);
                break;
            case R.id.tv_forget_password:
                intentJump(FindPasswordActivity.class);
                break;
            case R.id.titile_right_text:
                intentJumpForResult(RegisterActivity.class, 0);
                break;
            case R.id.titile_left_imageview:
                finish();
                break;


        }
    }


    /**
     * 登录成功之后跳转
     */
    @Override
    public void jumpActivity() {
        setJumpActivity();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        LogUtil.log("request:" + requestCode + " result:" + resultCode);

        if (resultCode == RESULT_OK)
            setJumpActivity();
    }

    public void setJumpActivity() {
        if (mBundle != null) {
            String toActivity = mBundle.getString("toActivity");
            try {
                if (TextUtils.isEmpty(toActivity)) {// 如果没有目标Activity
                    setResult(RESULT_OK);
                    finish();
                } else {// 有目标activity
                    Class toActivityClass = Class.forName(toActivity);
                    Bundle toActivityBundle = mBundle.getBundle("toActivityBundle");
                    intentJump(toActivityClass, toActivityBundle);
                    finish();

                }
            } catch (Exception e) {
                e.printStackTrace();
                setResult(RESULT_OK);
                finish();
            }
        } else {// 回到之前页面
            setResult(RESULT_OK);
            finish();
        }
    }

    @Override
    public void toast(String msg) {
        ToastUtil.show(msg, Toast.LENGTH_SHORT);

    }

    @Override
    public String getUserName() {
        return mEtUName.getText().toString();
//        return "13261422882";
    }

    @Override
    public String getPassword() {
        return mEtPassword.getText().toString();
//        return "2222212";
    }

}
